package com.gitee.wsl.text.regex

interface NamedRegexPart: RegexPart {
    /**
     * Name of the pattern, used to identify the capture group.
     * Should not include special characters and must be unique.
     */
    val name: String
}

/**
 * Groups a sequence of patterns into a single [Regex] where every capture group is identified by its token type (name).
 * @return a single [Regex] that captures all groups.
 */
fun Iterable<NamedRegexPart>.groupify(): Regex =
    this.joinToString(separator = "|") { pattern ->
        "(?<${pattern.name}>${pattern.regexStr()})"
    }.toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))